/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firstround2009;

import java.util.Arrays;
import java.io.IOException;
import static chanlib.Chan_IO.*;

/**
 * 50 mins of implementation.
 * quite long keep in mind t
 * @author robit
 */
public class TheNextNumber{
    /*
      All neccesary to be defined
      including:
      variable, inner class
    */
    // static int A;
    // static void B();
    // static class C

    /*
       One calculation fits all of the cases.
    */
    static void preReckon(){
        return;
    }

    /*
      calculate the answer
    */
    private static void reckon(int line) {
        String N=String();
        int n=N.length();
        int nn=n+1;
        char digs[]=new char[nn];
        digs[0]='0';
        for(int i=0;i<n;i++)digs[i+1]=N.charAt(i);

Outer:  for(int i=nn-2;i>=0;i--){
            int minmaxj=-1;
            char minmax='9'+1;
            for(int j=i+1;j<nn;j++){
                if(digs[j]>digs[i]&&digs[j]<minmax){
                    minmaxj=j;
                    minmax=digs[j];
                }
            }
            if(minmaxj!=-1){
                char tmp=digs[i];
                digs[i]=digs[minmaxj];
                digs[minmaxj]=tmp;
                Arrays.sort(digs, i+1, nn);
                break Outer;
            }
        }
        String ans;
        if(digs[0]=='0'){
            ans=new String(digs, 1, n);
        }else ans = new String(digs,0,nn);

        String output=String.format("Case #%d: %s",line,ans);
        writeln(output, OUTFILE);
        //writeln(String.format("N=%d K=%d\n%s", N, K, ans),ANALFILE);
        //println(output);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        /*
           Pre-computing for all cases
        */

        // Just in case!
        //preReckon();
        //while((char)read()!='P');

        /*
          Input-output
        */
        boolean test=false;
        boolean small=false;
        String prob="B";
        String filename;
        if(test) filename="test";
        else{
            if(small) filename=prob+"-small-practice";
            else filename=prob+"-large-practice";
        }
        try {
            /*
               Init IO,plus determination of input line number
             */
            int lineNums = initFileIO(filename);
            for (int i = 1; i <= lineNums; i++) {
                reckon(i);
            }
        } finally {
            endIO();
        }
    }

}
